<include file="Public:head" title='商品详情' />
<script src="__PUBLIC__/js/Jquery/validate.js"></script>
<script type="text/javascript">
    $(function() {
        jQuery.validator.addMethod("checkPrice", function(value, element) {
            var price = parseInt($("input[name=price]").val());
            var discount = parseInt($("input[name=discount]").val());
            if (discount > price) {
                return false;
            } else {
                return true;
            }

        });
        $("#form").validate({
            errorPlacement: function(error, element) {
                var error_td = element.parent('td');
                error_td.find('label.error').hide();
                error_td.append(error);
            },
            rules: {
                name: {
                    required: true
                },
                shop_id: {
                    min: 1
                },
                cat_id: {
                    min: 1
                },
                price: {
                    checkPrice: true,
                    required: true
                }

            },
            messages: {
                name: {
                    required: '商品名称不能为空'
                },
                shop_id: {
                    min: '请选择店铺'
                },
                cat_id: {
                    min: "请选择商品类别"
                },
                price: {
                    checkPrice: "折扣不能大于原价",
                    required: "价格不能为空"
                }

            }
        });
    });
    function getGoodsCat(id) {
        $.post(getUrl("Ajax/getGoodsCat"), {
            id: id
        }, function(data) {
            if (data) {
                var len = data.length;
                var option = '';
                for (var i = 0; i < len; i++) {
                    option += "<option value=" + data[i]['id'] + ">" + data[i]['name'] + "</option>";
                    $('#cat_id').html("<option value=0>请选择类别</option>" + option);
                }
            } else {
                $('#cat_id').html("<option value=0>请选择类别</option>");
            }

        }, "json")
    }
    function addGoodsCat(id, table) {
        var id = $('#shop_id').val();
        if (id == 0) {
            alert("请选择店铺");
            return false;
        }

        var title = $('#shop_id').find("option:selected").text();
        $("#modal_cat").find(".modal-title").text(title)
        $('#modal_cat').modal('toggle');
    }
    function addGoodsCatConfirm() {
        var name = $('#cat_name').val();
        if (name == '') {
            alert("名称不能为空");
            return false;
        }
        $.post(getUrl("Ajax/addGoodsCatConfirm"), {
            id: $('#shop_id').val(),
            name: name
        }, function(data) {
            $('#cat_id').prepend("<option value='" + data + "' selected>" + name + "</option>");
            $('#modal_cat').modal('toggle');
        })

    }
    
    function checkForm(){
        var id = "{$Think.get.id}";
        if(id == 0){
            var name = $("#name").val();
            if(name == ''){
                alert("请输入商品名称！");
                return false;
            }
        }
    }
</script>
<div class="main-container" id="main-container">
    <div class="main-container-inner">
        <include file="Public:left" />
        <div class="main-content">
            <div class="breadcrumbs" id="breadcrumbs">
                <ul class="breadcrumb">
                    <li><i class="icon-home home-icon"></i><a href="__APP__/{$Think.MODULE_NAME}">首页</a></li>
                    <li><a href="{:U('Goods/lists')}">商品管理</a></li>
                    <li class="active">商品列表</li>
                </ul>
            </div>
            <div class="page-content">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="row">
                            <div class="col-xs-12">
                                <h3 class="header smaller lighter blue">商品{$Think.get.id|getPageType}</h3>

                                <form action="{:U('Goods/detail_post')}" method="POST" id="form" onsubmit="checkForm()" enctype="multipart/form-data" >
                                    <table class="table table_detail">
                                        <tbody>
                                            <tr>
                                                <td class="td_left">名称:</td>
                                                <td>
                                        <if condition="$detail.id gt 0">
                                            <input name="name" type="text" class="common_txt" id="name"  value="{$detail.name}" maxlength="30" autocomplete="off"/>
                                            <else/>
                                            <input name="name[]"id="name" type="text" class="common_txt"  value="{$detail.name}" maxlength="30" autocomplete="off"/>
                                            <a onclick="addNames()">+添加名称</a>
                                        </if>
                                        </td>
                                        </tr>
                                        <tr id="tr_name">
                                            <td class="td_left"> 所属店铺:</td>
                                            <td>
                                                <select name="shop_id" id="shop_id" onchange="getGoodsCat(this.value)" autocomplete="off">
                                                    <option value="0">请选择店铺</option>
                                                    <foreach name="shops" item="row">
                                                        <option value="{$row.id}"<if condition="$row.id eq $detail['sid']">selected</if>>{$row.name}</option>
                                                    </foreach>
                                                </select>
                                                <!--                                        <a onclick="choseShop('')">高级搜索店铺</a>-->
                                            </td>
                                        </tr>

                                        <tr>
                                            <td class="td_left">商品类别:</td>
                                            <td>
                                                <select name="cat_id" id="cat_id" autocomplete="off">
                                                    <option value="0">请选择类别</option>
                                                    <foreach name="cats" item="row">
                                                        <option value="{$row.id}"<if condition="$row.id eq $detail['cat_id']">selected</if>>{$row.name}</option>
                                                    </foreach>
                                                </select>
                                                <a onclick="addGoodsCat()">新增类别</a>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="td_left">上传图片:</td>
                                            <td>
                                                <input name="logo" type="file" id="logo" />
                                            </td>
                                        </tr>
                                        <if condition="$detail.id gt 0 && $detail.logo neq ''">
                                            <tr  id="td_img">
                                                <td class="td_left"></td>
                                                <td>
                                                    <img src="__APP__/{$Think.config.goodsFile}{$detail.logo}" style="max-width: 300px;max-height: 200px"/> 
                                                    <a onclick="delPic('{$detail.id}', 'goods', 'logo')">删除</a>
                                                </td>
                                            </tr>
                                        </if>
                                        <tr>
                                            <td class="td_left">标签</td>
                                            <td style="text-align: left">
                                        <foreach name="attr" item="row"> 
                                            <input type="checkbox" name="attr[]"  autocomplete="off" 
                                                   value="{$row.id}" {$detail.attr|getChecked=###,$row['id']}>
                                            <img src="__APP__/{$Think.config.shopsFile}{$row.logo}" />{$row.name}
                                        </foreach>
                                        </td>
                                        </tr>
                                        <tr>
                                            <td class="td_left">价格:</td>
                                            <td>
                                                <input name="price" style="width:90px;" type="text" class="common_txt" value="{$detail.price}" onblur="getMinInt($(this), '');" autocomplete="off" />
                                                元
                                                <input type="checkbox" name="discountCheckbox" autocomplete="off" 
                                        <if condition="$detail.discount gt 0">checked</if> onclick="showCheckbox($(this),$('#discountArea'))"> 减价
                                        <span id="discountArea" <if condition="$detail.discount eq  0">style='display:none'</if>>
                                            <input name="discount" style="width:90px;" type="text" class="common_txt" value="{$detail.discount}" autocomplete="off" /> 
                                            元
                                        </span>
                                        </td>
                                        </tr>
                                        <tr>
                                            <td class="td_left">单位:</td>
                                            <td>
                                                <span class='g-input-selectbox'>
                                                    <select name="units" id="units" autocomplete="off">
                                                        <foreach name="units" item="row">
                                                            <option value="{$row.id}"<if condition="$row.id eq $detail['unit']">selected</if>>{$row.name}</option>
                                                        </foreach>
                                                    </select>
                                                </span> 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="td_left">描述:</td>
                                            <td>
                                                <textarea  name="intro" style="width:410px;height:50px;">{$detail.intro}</textarea>

                                            </td>
                                        </tr>

                                        <tr>
                                            <td class="td_left">排序:</td>
                                            <td>
                                                <input name="ord" type="text" class="common_txt" value="{$detail.ord}" />
                                            </td>
                                        </tr>


                                        <tr>
                                            <td class="td_left">是否开启:</td>
                                            <td>
                                                <input type="radio" name="is_check" value="0" autocomplete="off" 
                                        <if condition="$detail.is_check eq 0">checked</if>> 否
                                        <input type="radio" name="is_check" value="1" autocomplete="off" 
                                               <if condition="$detail.is_check eq 1">checked</if>> 是
                                        </td>
                                        </tr>
                                        <tr>
                                            <td class="td_left"></td>
                                            <td>
                                                <input type='hidden' name='id' value='{$Think.get.id}'/>
                                                <button class="btn btn-info btn-sm" type="submit">
                                                    <i class="icon-ok bigger-110 "></i>
                                                    保 存
                                                </button>
                                                <button class="btn btn-sm reset" type="button" onclick="goUrl(-1)">
                                                    <i class="icon-undo bigger-110"></i>
                                                    返 回
                                                </button>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <include file="Public:setting" />
    </div>
</div>
<div class="modal fade" id="modal_cat" data-id="0" data-modal="position"  tabindex="-1" role="dialog" aria-labelledby="modal_catLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" ></h4>
            </div>
            <div class="modal-body">
                名称：
                <input id="cat_name" type="text" class="common_txt" value="{$detail.ord}" maxlength="20" autocomplete="off"/>
            </div>
            <div class="modal-footer">
                <button class="btn btn-danger btn-xs ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false" onclick="addGoodsCatConfirm()">
                    <span class="ui-button-text">
                        <i class="icon-trash bigger-110"></i>
                        确 定
                    </span>
                </button>
                <button class="btn btn-xs ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false" data-dismiss="modal">
                    <span class="ui-button-text">
                        <i class="icon-remove bigger-110"></i>
                        取 消
                    </span>
                </button>
            </div>
        </div>
    </div>
</div>
<include file="Public:foot" />